package com.ruoyi.basedata.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Objects;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.basedata.mapper.XbCommandLogsMapper;
import com.ruoyi.basedata.domain.XbCommandLogs;
import com.ruoyi.basedata.service.IXbCommandLogsService;

/**
 * 小表开关阀日志Service业务层处理
 *
 * @author gdqt
 * @date 2024-03-13
 */
@Service
public class XbCommandLogsServiceImpl implements IXbCommandLogsService {
    @Autowired
    private XbCommandLogsMapper xbCommandLogsMapper;

    /**
     * 查询小表开关阀日志
     *
     * @param id 小表开关阀日志主键
     * @return 小表开关阀日志
     */
    @Override
    public XbCommandLogs selectXbCommandLogsById(Long id) {
        return xbCommandLogsMapper.selectXbCommandLogsById(id);
    }

    /**
     * 查询小表开关阀日志列表
     *
     * @param xbCommandLogs 小表开关阀日志
     * @return 小表开关阀日志
     */
    @Override
    public List<XbCommandLogs> selectXbCommandLogsList(XbCommandLogs xbCommandLogs) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        if (!Objects.equals(user.getSystemFlag(),Constants.SYS_FLAG_ADMIN)){
            xbCommandLogs.setSysUserId(user.getUserId());
        }
        if(Objects.equals(user.getSystemFlag(), Constants.SYS_FLAG_NORMAL_USER)){
            xbCommandLogs.setTenantId(user.getTenantId());
        }
        return xbCommandLogsMapper.selectXbCommandLogsList(xbCommandLogs);
    }

    /**
     * 新增小表开关阀日志
     *
     * @param xbCommandLogs 小表开关阀日志
     * @return 结果
     */
    @Override
    public int insertXbCommandLogs(XbCommandLogs xbCommandLogs) {
        xbCommandLogs.setCreateTime(DateUtils.getNowDate());
        return xbCommandLogsMapper.insertXbCommandLogs(xbCommandLogs);
    }

    /**
     * 修改小表开关阀日志
     *
     * @param xbCommandLogs 小表开关阀日志
     * @return 结果
     */
    @Override
    public int updateXbCommandLogs(XbCommandLogs xbCommandLogs) {
        return xbCommandLogsMapper.updateXbCommandLogs(xbCommandLogs);
    }

    /**
     * 批量删除小表开关阀日志
     *
     * @param ids 需要删除的小表开关阀日志主键
     * @return 结果
     */
    @Override
    public int deleteXbCommandLogsByIds(Long[] ids) {
        return xbCommandLogsMapper.deleteXbCommandLogsByIds(ids);
    }

    /**
     * 删除小表开关阀日志信息
     *
     * @param id 小表开关阀日志主键
     * @return 结果
     */
    @Override
    public int deleteXbCommandLogsById(Long id) {
        return xbCommandLogsMapper.deleteXbCommandLogsById(id);
    }

    @Override
    public void saveXbCommandLog(String terminalId, int command, String feedback) {
        XbCommandLogs log = new XbCommandLogs();
        log.setCommand(command);
        log.setTerminalId(terminalId);
        log.setFeedback(feedback);
        log.setSendTime(new Date());
        xbCommandLogsMapper.insertXbCommandLogs(log);
    }
}
